package group2projman;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class LibrarySystem implements DbWritable {

	/**
	 * TODO: 'splain, this is a list of types for which we handle
	 * refs and stuff. Used as indices in array currentId[] to get
	 * IDs for them, and stuff.
	 */
	public enum Type {
		TRANSACTION
	}
	/// In pence
	static public final int DAILY_OVERDUE_COST = 25;  		
	
	private static LibrarySystem instance = null;
	
	// ID->REFERENCE MAPS
	Map<Integer, Book> books = new HashMap<Integer, Book>();
	
	Map<Integer, Member> members = new HashMap<Integer, Member>();
	/*
	 * TODO: worry about asymmetry - books accessed properly, though
	 * catalogue, by isbn. Their copies, ostensibly managed by the book objects,
	 * are accessed directly via this map.
	 */
	Map<Integer, BookCopy> bookCopies = new HashMap<Integer, BookCopy>();
	// ID->REFERENCE MAPS
	
	// ID COUNTERS
	// TODO: stacks of trashed object ids for reuse?
	private int[] currentId = new int[1];
	
	// ID COUNTERS
	
	Catalogue catalogue;
	
	static public LibrarySystem getInstance() {
		return instance;
	}
	
	static public void setInstance(LibrarySystem pInstance) {
		instance = pInstance;
	}
	
	// TODO: consider throwing an exception if result is null
	public Member getMember(int id) {
		Member result = members.get(id);
		
		return result;
	}
	
	// TODO: consider throwing an exception if result is null
	public Book getBook(int id) {
		return catalogue.searchBooks(Query.byIsbn(id))[0];
	}		
	
	public int nextId(Type type) {
		++currentId[type.ordinal()];
		
		return currentId[type.ordinal()];
	}	

}
